package com.vhans.db.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vhans.core.domain.Storage;
import com.vhans.core.utils.BeanCopyUtils;
import com.vhans.db.mapper.StorageMapper;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.dto.storage.StorageDTO;
import com.vhans.db.model.vo.storage.StorageInfoVO;
import com.vhans.db.model.vo.storage.StorageVO;
import com.vhans.db.service.IStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 存储Service业务层处理
 *
 * @author vhans
 * @date 2023-05-20
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {
    @Autowired
    private StorageMapper storageMapper;

    /**
     * 查询存储列表
     *
     * @param condition 条件
     * @return 存储
     */
    @Override
    public List<StorageVO> selectStorageVOList(ConditionDTO condition) {
        // 查询存储信息
        return storageMapper.selectStorageVOList(condition);
    }

    /**
     * 新增存储
     *
     * @param storage 存储
     * @return 插入id
     */
    @Override
    public Integer insertStorage(StorageDTO storage) {
        // 要添加的存储
        Storage newStorage = BeanCopyUtils.copyBean(storage, Storage.class);
        storageMapper.insert(newStorage);
        return newStorage.getId();
    }

    /**
     * 获取存储编辑信息
     *
     * @param id 存储主键
     * @return 存储
     */
    @Override
    public StorageInfoVO selectStorageInfoById(Integer id) {
        // 查询存储信息
        StorageInfoVO storageInfoVO = storageMapper.selectStorageInfoById(id);
        Assert.notNull(storageInfoVO, "没有该存储");
        return storageInfoVO;
    }

    /**
     * 修改存储
     *
     * @param storage 存储
     * @return 影响行
     */
    @Override
    public int updateStorage(StorageDTO storage) {
        // 要修改的存储
        Storage newStorage = BeanCopyUtils.copyBean(storage, Storage.class);
        return storageMapper.updateById(newStorage);
    }

    /**
     * 批量删除存储
     *
     * @param idList 需要删除的存储主键列表
     * @return 影响行
     */
    @Override
    public int deleteStorageByIdIdList(List<Integer> idList) {
        // 删除存储
        return storageMapper.deleteBatchIds(idList);
    }
}
